package com.itheima.util;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.HashMap;
import java.util.Map;

//JWT的生成和解析
public class JwtUtil {
    // 创建token
    public static String createToken(Map claims) {
        return Jwts.builder()
                .setClaims(claims) //设置响应数据体
                .signWith(SignatureAlgorithm.HS256, "heima_movies") //设置加密方法和加密盐
                .compact();
    }

    // 解析token
    public static Map parseToken(String token) {
        return Jwts.parser().setSigningKey("heima_movies")
                .parseClaimsJws(token)
                .getBody();
    }

    // 加密base64enc(HS256)  和解密：base64enc
    public static void main(String[] args) {

        Map<String ,String > user =new HashMap<>();
        user.put("lanxiaopeng","蓝晓耳畔过");
        user.put("兰大","aljfdka");
        String token = createToken(user);
        System.out.println(token);

        Map map = parseToken(token);
        System.out.println(map);
    }
}
